/*
 * @(#)TasksType.java
 *
 * 
 *
 * ver 1.0 Aug 16, 2007 ggranum
 */
package org.testfire.model;

import org.testfire.model.assertions.AssertionType;
import org.testfire.model.exception.CannotParseObjectException;
import org.testfire.translator.TranslatorFactory;
import org.testfire.util.DomUtil;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;

public class TasksType implements TestFireObject
{

    List<TestFireObject> children = new ArrayList<TestFireObject>();

    TestFireObject parent;

    public TasksType( Element element )
    {
        try
        {
            this.parse( element );
        }
        catch ( CannotParseObjectException e )
        {
            e.printStackTrace();
        }
    }

    

    public void setParent( TestFireObject parent )
    {
        this.parent = parent;
    }

    public TestFireObject getParent()
    {
        return parent;
    }

    public void parse( Element el ) throws CannotParseObjectException
    {
        List<Element> childs = DomUtil.getChildElements( el );
        for ( Element child : childs )
        {
            String name = child.getNodeName();
            if ( name.indexOf( "ElementAction" ) >= 0 )
            {
                children.add( new ElementActionType( child ) );
            }
            else if ( name.indexOf( "BrowserCommand" ) >= 0 )
            {
                children.add( new HtmlFinderType( child ) );
            }
            else if ( name.indexOf( "Assertion" ) >= 0 )
            {
                children.add( new AssertionType( child ) );
            }
            else if ( name.indexOf("taskGroup") >= 0 )
            {
                parse(child); // recurse to flatten; groups are for users' sanity.
            }
        }
    }


    public Object translate( TestsetType testset )
    {
        return TranslatorFactory.translate(testset, this);
    }

    public List<TestFireObject> getChildren()
    {
        return children;
    }
}

// eof: TasksType.java